<template>
  <div>
    <!-- 税收编码选择弹框 -->
    <el-dialog title="税收编码选择" class="dialog-wrapper" :visible.sync="dialog" width="50%" append-to-body :closeOnClickModal="false" :closeOnPressEscape="false">
      <div style="margin-top: -20px">
        <el-input placeholder="请输入税收分类编码名称/合并编码" v-model="content" class="input-with-select">
          <el-button slot="append" icon="el-icon-search" @click="searchClassificationCode()"></el-button>
        </el-input>
        <div style="margin: 1% 0 0 0; height: 35px; border: 1px solid #b2d6ff; background-color: #ebf5ff">
          <span style="height: 35px; line-height: 35px; margin-left: 2%; font-size: 12.5px">
            <i class="el-icon-warning" style="color: #1890ff; font-size: small"></i>
            <span style="margin-left: 7px"> 请双击选择税收编码 </span>
          </span>
        </div>
        <el-table
          ref="singleTable"
          :data="tableData"
          border
          stripe
          highlight-current-row
          max-height="500px"
          size="small"
          style="width: 100%"
          class="tableSy"
          onblur="click-id=''"
          @row-dblclick="dbClickFn"
        >
          <el-table-column label="合并编码" width="160" prop="classificationCode" header-align="center" show-overflow-tooltip />
          <el-table-column label="名称" prop="classificationName" header-align="center" show-overflow-tooltip />
          <el-table-column label="简称" prop="classificationAbbr" header-align="center" show-overflow-tooltip />
          <el-table-column label="说明" width="250" prop="description" header-align="center" show-overflow-tooltip />
          <el-table-column label="税率" width="60" prop="taxRateList" header-align="center" show-overflow-tooltip />
        </el-table>

        <!-- 分页页码 -->
        <div class="block">
          <el-pagination
            :small="true"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="this.pageParams.pageIndex"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="this.pageParams.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="this.pageParams.total"
          ></el-pagination>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { productCode, codeDetails } from '@/api/output/issue.js'
export default {
  data() {
    return {
      dialog: false,
      content: '',
      tableData: [],
      pageParams: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    }
  },
  methods: {
    searchClassificationCode() {
      let param = {
        classificationName: '',
        classificationCode: '',
        pageIndex: 1,
        pageSize: null,
      }
      param.classificationName = this.content
      param.classificationCode = this.content
      param.pageIndex = this.pageParams.pageIndex
      param.pageSize = this.pageParams.pageSize
      productCode(param).then((res) => {
        if (res.code == 0) {
          this.tableData = res.data.rows
          this.pageParams.total = Number(res.data.total)
        } else {
          this.$message({
            showClose: true,
            message: res.message,
            type: 'error',
          })
        }
      })
    },
    dbClickFn(data) {
      if (data) {
        let param = {
          classificationCode: '',
        }
        param.classificationCode = data.classificationCode
        codeDetails(param).then((res) => {
          if (res.code == 0) {
            this.$parent.$parent.$parent.goodsForm.classificationCode = res.data.classificationCode
            this.$parent.$parent.$parent.goodsForm.classificationName = res.data.classificationName
            
            this.$parent.$parent.$parent.goodsForm.classificationAbbr = res.data.classificationAbbr
            this.$parent.$parent.$parent.goodsForm.classificationVersion = res.data.classificationVersion
            this.$parent.$parent.$parent.goodsForm.taxRate = res.data.taxRate

            
            let jsonParm = res.data.taxRateValueList
            const map = new Map()
            for (var i = 0; i < jsonParm.length; i++) {
              var parm = JSON.parse(jsonParm[i]) * 100 + '%'
              var taxParm = {
                value: jsonParm[i],
                label: parm,
              }
              this.$parent.$parent.$parent.taxRateOptions.push(taxParm)
            }
            this.$parent.$parent.$parent.taxRateOptions = this.$parent.$parent.$parent.taxRateOptions.filter((key) => !map.has(key.value) && map.set(key.value, 1))
            this.dialog = false
            this.$parent.$parent.$parent.autocalculatebackstage('amountExcTaxChange')
          } else {
            this.$message({
              showClose: true,
              message: res.message,
              type: 'error',
            })
          }
        })
      }
    },
    handleSizeChange(val) {
      this.pageParams.pageSize = val
      this.searchClassificationCode()
    },
    handleCurrentChange(val) {
      this.pageParams.pageIndex = val
      this.searchClassificationCode()
    },
  },
  created() {
    this.searchClassificationCode()
  },
}
</script>